package factorytest.iflytek.com.einkiflytekfactorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;


import androidx.core.app.ActivityCompat;
import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.common.LedCtrl;
import factorytest.iflytek.com.einkiflytekfactorytest.config.EinkProperty;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DebugLog;

public class KeyAndLedActivity extends Activity {

    private final static String TAG = "KeyAndLedActivity";

    private Button btnPower;
    private Button btnHome;
    private Button btnHall;

    private Button btnLedGreen;
    private Button btnLedRed;
    private Button btnLedClose;

    private Button btnPass;
    private Button btnFail;

    private Context mContext;

    private final int FLAG_KEY_POWER = 0x01;
    private final int FLAG_KEY_HOME = 0x02;
    private final int FLAG_KEY_HALL = 0x04;

    private final int FLAG_LED_RED = 0x10;
    private final int FLAG_LED_GREEN = 0x20;
    private final int FLAG_LED_CLOSE = 0x40;


    private int flagKeyTest = FLAG_KEY_POWER | FLAG_KEY_HOME | FLAG_KEY_HALL | FLAG_LED_RED | FLAG_LED_GREEN | FLAG_LED_CLOSE;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_key_led);

        mContext = this;

        btnPower = (Button) findViewById(R.id.btnPower);
        btnHome = (Button) findViewById(R.id.btnHome);
        btnHall = (Button) findViewById(R.id.btnHall);

        btnLedGreen = (Button) findViewById(R.id.btnLedGreen);
        btnLedRed = (Button) findViewById(R.id.btnLedRed);
        btnLedClose = (Button) findViewById(R.id.btnLedClose);

        btnPass= (Button) findViewById(R.id.btnPass);
        btnFail= (Button) findViewById(R.id.btnFail);

        btnLedGreen.setOnClickListener(clickListener);
        btnLedRed.setOnClickListener(clickListener);
        btnLedClose.setOnClickListener(clickListener);
        btnPass.setOnClickListener(clickListener);
        btnFail.setOnClickListener(clickListener);

        btnPass.setEnabled(false);

    }

    @Override
    protected void onResume() {
        super.onResume();
        EinkProperty.setEinkFactoryTestEnabled(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EinkProperty.setEinkFactoryTestEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (v == btnLedGreen) {

                LedCtrl.setLed(true, LedCtrl.LedAttr.led_green);

                flagKeyTest &= ~FLAG_LED_GREEN;

            } else if (v == btnLedRed) {

                LedCtrl.setLed(true, LedCtrl.LedAttr.led_red);

                flagKeyTest &= ~FLAG_LED_RED;


            } else if (v == btnLedClose) {

                LedCtrl.setLed(false, LedCtrl.LedAttr.led_red, LedCtrl.LedAttr.led_green);


                flagKeyTest &= ~FLAG_LED_CLOSE;

            } else if (v == btnPass) {

                finishWithOK();

            } else if (v == btnFail) {

                finishWithFail();

            }

            if (flagKeyTest == 0) {
                btnPass.setEnabled(true);
            }

        }
    };

    private void finishWithOK() {
        Toast.makeText(mContext, R.string.test_ok, Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK);
        ActivityCompat.finishAfterTransition(this);
    }

    private void finishWithFail() {
        Toast.makeText(mContext, R.string.test_fail, Toast.LENGTH_SHORT).show();
        setResult(RESULT_CANCELED);
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        DebugLog.w("onKeyDown keycode = " + keyCode + ", key event = " + event + "key action = " + event.getAction());

        if (keyCode == KeyEvent.KEYCODE_HOME) {

            btnHome.setSelected(true);

            return true;

        } else if (keyCode == KeyEvent.KEYCODE_POWER) {

            btnPower.setSelected(true);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_SLEEP) {

            btnHall.setSelected(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        Log.e(TAG, "onKeyUp keycode = " + keyCode + ", key event = " + event);

        boolean ret = false;

        if (keyCode == KeyEvent.KEYCODE_HOME) {

            btnHome.setSelected(false);
            btnHome.setText(getNewTextForBtn(btnHome, getResources().getString(R.string.test_ok)));

            flagKeyTest &= ~FLAG_KEY_HOME;

            ret = true;

        } else if (keyCode == KeyEvent.KEYCODE_POWER) {

            btnPower.setSelected(false);
            btnPower.setText(getNewTextForBtn(btnPower, getResources().getString(R.string.test_ok)));

            flagKeyTest &= ~FLAG_KEY_POWER;
            ret = true;

        } else if (keyCode == KeyEvent.KEYCODE_SLEEP) {

            btnHall.setSelected(false);
            btnHall.setText(getNewTextForBtn(btnHall, getResources().getString(R.string.test_ok)));

            flagKeyTest &= ~FLAG_KEY_HALL;
            ret = true;

//        } else if (keyCode == KeyEvent.KEYCODE_WAKEUP) {
//
//            btnHall.setSelected(false);
//            btnHall.setText(getNewTextForBtn(btnHall, getResources().getString(R.string.test_ok)));
//
//            flagKeyTest &= ~FLAG_KEY_HALL;
//
//            ret = true;
        }

        if (ret && flagKeyTest == 0) {
            btnPass.setEnabled(true);
        }

        return ret || super.onKeyUp(keyCode, event);
    }

    private String getNewTextForBtn(Button btn , String test_status) {
        String text = btn.getText().toString();
        String arrow = getResources().getString(R.string.arrow);
        String[] rmStatus = text.split(arrow);
        if (rmStatus.length > 1) {
            text = rmStatus[0];
        }
        return (text + arrow + test_status);
    }



}
